package shapewar.view.effect;

import android.graphics.Canvas;
import shapewar.engine.calc.Pos;
import shapewar.engine.intefac.Regedit;
import shapewar.engine.model.abst.Model;
import shapewar.engine.model.abst.ModelAdapter;
import shapewar.engine.presenter.EngineThread;
import shapewar.engine.presenter.Presenter;
import shapewar.view.myview.MyView;

public class EffectView extends MyView{
	final int Flash_Effect = 1;
	final int Bullet_Die_Effect = 2;
	public int effect;
	private float radius; 
	int alpha = 255;
	int t = 3;
	public EffectView(Presenter pst, Model shape) {
		super(pst, new ModelAdapter(pst,shape._id,shape._x,shape._y,shape.v,shape._depth){
			@Override
			public void init() {
				pst.regist(this, Regedit.IRepeat4Effect,Regedit.IView);
			}
			@Override
			public void destory() {
				pst.registoff(this, Regedit.IRepeat4Effect,Regedit.IView);
			}
			@Override
			public void repeat() {
				pst.bridge.invalidate(this);
			}
		});
		getModel().init();
		getModel()._rotation = shape._rotation;
		getModel().r = shape.r;
	}
	
	public void createFlashEffect(Pos oldPos){
		pst.bridge.addView(this);
		alpha = 255;
		t = 1;
		getModel()._x = oldPos.x;
		getModel()._y = oldPos.y;
		effect = Flash_Effect;
	}
	
	public void createBulletDieEffect(Pos oldPos){
		pst.bridge.addView(this);
		alpha = 255;
		t = 1;
		getModel()._x = oldPos.x;
		getModel()._y = oldPos.y;
		effect = Bullet_Die_Effect;
	}
	
	@Override
	public void invalidate() {
		super.invalidate();
		if(alpha<5){
			pst.bridge.removeView(this);
			getModel().destory();
		}
		if(radius>30){
			pst.bridge.removeView(this);
			getModel().destory();
		}
	}
	@Override
	protected void onDraw(Canvas canvas) {
		if (!isNearFrameWindow()){
			alpha = 0;
			return;
		}
		switch(effect){
		case Flash_Effect:
			t++;
			alpha = 255/t;
			paint.setAlpha(alpha);
			super.onDraw(canvas);
			break;
		case Bullet_Die_Effect:
			t = t+6;
			alpha = 255/t;
			radius = getModel().r * rate * t;
			paint.setAlpha(alpha);
			canvas.drawCircle(getRelativePos().x, getRelativePos().y, radius * rate, paint);
			break;
		}
	}
}

